package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    private OrdersMapper ordersMapper;
    //处理超时订单
    @Scheduled(cron = "0 0/1 * * * ?")
    public void orderTimeOut(){
        log.info("处理超时订单");
        Integer status = Orders.PENDING_PAYMENT;
        LocalDateTime time = LocalDateTime.now().plusMinutes(-15);
        List<Orders> orders = ordersMapper.getTimeOutOrders(status,time);
        for(Orders order:orders){
            order.setStatus(Orders.CANCELLED);
            order.setCancelTime(LocalDateTime.now());
            order.setCancelReason("超时未支付");
            ordersMapper.update(order);
        }
    }

    //凌晨一点处理派送中订单
    @Scheduled(cron = "0 0 1 * * ?")
    public void orderDeliver(){
        log.info("处理派送中订单");
        //前一天的时间
        LocalDateTime time=LocalDateTime.now().plusMinutes(-60);
        Integer status=Orders.DELIVERY_IN_PROGRESS;
        List<Orders> orders=ordersMapper.getTimeOutOrders(status,time);
        for(Orders order:orders){
            order.setStatus(Orders.COMPLETED);
            ordersMapper.update(order);
        }
    }

}
